{% extends "base.html" %}

{% block title %}线索管理 - {% if followup %}编辑线索跟进记录{% else %}添加线索跟进记录{% endif %}{% endblock %}

{% block page_title %}{% endblock %}
{% block page_subtitle %}{% endblock %}

{% block content %}
<div class="container mx-auto space-y-6">
    <!-- 面包屑导航 -->
    <nav class="text-sm breadcrumbs mb-6">
        <ol class="flex items-center space-x-2">
            <li><a href="{{ url_for('main.dashboard') }}" class="text-gray-500 hover:text-gray-700">首页</a></li>
            <li><i class="fas fa-chevron-right text-gray-400 text-xs mx-2"></i></li>
            <li><a href="{{ url_for('lead.lead_list') }}" class="text-gray-500 hover:text-gray-700">线索管理</a></li>
            <li><i class="fas fa-chevron-right text-gray-400 text-xs mx-2"></i></li>
            <li><a href="{{ url_for('lead.lead_detail', lead_id=lead.id) }}" class="text-gray-500 hover:text-gray-700">{{ lead.name }}</a></li>
            <li><i class="fas fa-chevron-right text-gray-400 text-xs mx-2"></i></li>
            <li class="text-gray-700">{% if followup %}编辑跟进记录{% else %}添加跟进记录{% endif %}</li>
        </ol>
    </nav>

    <div class="bg-white rounded-lg shadow">
        <div class="p-6">
            <form method="post" enctype="multipart/form-data">
                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                <div class="grid grid-cols-1 gap-6">
                    <!-- 基本信息 -->
                    <div>
                        <h3 class="text-lg font-medium text-gray-900 mb-4">基本信息</h3>
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                            <div>
                                <label for="method" class="block text-sm font-medium text-gray-700">跟进方式 <span class="text-red-500">*</span></label>
                                <select name="method" id="method" required
                                    class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-primary focus:border-primary">
                                    <option value="">请选择跟进方式</option>
                                    <option value="电话" {% if followup and followup.method == '电话' %}selected{% endif %}>电话</option>
                                    <option value="邮件" {% if followup and followup.method == '邮件' %}selected{% endif %}>邮件</option>
                                    <option value="微信" {% if followup and followup.method == '微信' %}selected{% endif %}>微信</option>
                                    <option value="上门拜访" {% if followup and followup.method == '上门拜访' %}selected{% endif %}>上门拜访</option>
                                    <option value="会议" {% if followup and followup.method == '会议' %}selected{% endif %}>会议</option>
                                    <option value="其他" {% if followup and followup.method == '其他' %}selected{% endif %}>其他</option>
                                </select>
                            </div>
                            <div>
                                <label for="next_followup_time" class="block text-sm font-medium text-gray-700">下次跟进时间</label>
                                <input type="datetime-local" name="next_followup_time" id="next_followup_time"
                                    class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-primary focus:border-primary"
                                    value="{{ followup.next_followup_time.strftime('%Y-%m-%dT%H:%M') if followup and followup.next_followup_time else '' }}">
                            </div>
                            <div>
                                <label for="result" class="block text-sm font-medium text-gray-700">跟进结果</label>
                                <select name="result" id="result"
                                    class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-primary focus:border-primary">
                                    <option value="">请选择跟进结果</option>
                                    <option value="积极回应" {% if followup and followup.result == '积极回应' %}selected{% endif %}>积极回应</option>
                                    <option value="一般回应" {% if followup and followup.result == '一般回应' %}selected{% endif %}>一般回应</option>
                                    <option value="无回应" {% if followup and followup.result == '无回应' %}selected{% endif %}>无回应</option>
                                    <option value="拒绝" {% if followup and followup.result == '拒绝' %}selected{% endif %}>拒绝</option>
                                    <option value="需再次跟进" {% if followup and followup.result == '需再次跟进' %}selected{% endif %}>需再次跟进</option>
                                </select>
                            </div>
                        </div>
                    </div>

                    <!-- 跟进内容 -->
                    <div>
                        <h3 class="text-lg font-medium text-gray-900 mb-4">跟进内容</h3>
                        <div>
                            <label for="content" class="block text-sm font-medium text-gray-700">跟进内容 <span class="text-red-500">*</span></label>
                            <textarea name="content" id="content" rows="6" required
                                class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-primary focus:border-primary">{{ followup.content if followup else '' }}</textarea>
                        </div>
                    </div>

                    <!-- 线索状态更新 -->
                    <div>
                        <h3 class="text-lg font-medium text-gray-900 mb-4">线索状态更新</h3>
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                            <div>
                                <label for="update_status" class="block text-sm font-medium text-gray-700">线索状态</label>
                                <select name="update_status" id="update_status"
                                    class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-primary focus:border-primary">
                                    <option value="">不更新</option>
                                    <option value="新线索" {% if lead.status == '新线索' %}selected{% endif %}>新线索</option>
                                    <option value="跟进中" {% if lead.status == '跟进中' %}selected{% endif %}>跟进中</option>
                                    <option value="已转化" {% if lead.status == '已转化' %}selected{% endif %}>已转化</option>
                                    <option value="已关闭" {% if lead.status == '已关闭' %}selected{% endif %}>已关闭</option>
                                </select>
                            </div>
                            <div>
                                <label for="update_priority" class="block text-sm font-medium text-gray-700">优先级</label>
                                <select name="update_priority" id="update_priority"
                                    class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-primary focus:border-primary">
                                    <option value="">不更新</option>
                                    <option value="高" {% if lead.priority == '高' %}selected{% endif %}>高</option>
                                    <option value="中" {% if lead.priority == '中' %}selected{% endif %}>中</option>
                                    <option value="低" {% if lead.priority == '低' %}selected{% endif %}>低</option>
                                </select>
                            </div>
                        </div>
                    </div>

                    <!-- 转化客户 -->
                    {% if not lead.converted_to_customer_id %}
                    <div>
                        <h3 class="text-lg font-medium text-gray-900 mb-4">转化客户</h3>
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                            <div>
                                <label class="inline-flex items-center">
                                    <input type="checkbox" name="convert_to_customer" value="1" class="rounded border-gray-300 text-primary focus:ring-primary">
                                    <span class="ml-2 text-sm text-gray-700">将此线索转化为客户</span>
                                </label>
                            </div>
                            <div>
                                <label for="customer_type" class="block text-sm font-medium text-gray-700">客户类型</label>
                                <select name="customer_type" id="customer_type"
                                    class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-primary focus:border-primary">
                                    <option value="潜在客户">潜在客户</option>
                                    <option value="意向客户">意向客户</option>
                                    <option value="成交客户">成交客户</option>
                                </select>
                            </div>
                            <div class="md:col-span-2">
                                <label for="conversion_reason" class="block text-sm font-medium text-gray-700">转化原因</label>
                                <textarea name="conversion_reason" id="conversion_reason" rows="3"
                                    class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-primary focus:border-primary"
                                    placeholder="请输入转化原因，例如：客户需求明确、预算充足、决策链清晰等"></textarea>
                            </div>
                        </div>
                    </div>
                    {% endif %}
                </div>

                <div class="mt-6 flex justify-end space-x-3">
                    <a href="{{ url_for('lead.lead_detail', lead_id=lead.id) }}" class="px-4 py-2 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary">
                        取消
                    </a>
                    <button type="submit" class="px-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-primary hover:bg-blue-600 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary">
                        {% if followup %}保存修改{% else %}添加跟进记录{% endif %}
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
{% endblock %}